<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="数据日期" :required="true">
              <j-date placeholder="请选择数据日期" v-model="queryParam.sjrq"></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="主题">
              <j-tree-select
                ref="treeSelect"
                placeholder="请选择主题"
                v-model="queryParam.theme"
                dict="llbb_param_config,name,code"
                pidValue="1506488476828733442"
                hasChildField="has_child"
              >
              </j-tree-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
             <!-- <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>-->
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="bathSave" type="primary" v-has="'monthReport:bathSave'" >批量生成</a-button>
      <!-- <a-button @click="bathSaveTest" type="primary" v-has="'monthReport:bathSave'" >批量生成(测试专用)</a-button> -->
<!--      <a-button @click="bathCreate" type="primary"  >报文数据生成</a-button>-->
<!--      <a-button @click="mothCheck" type="primary" >批量校验</a-button>-->
<!--      <a-button @click="mothDown" type="primary" v-has="'monthReport:dayDown'" >批量下载</a-button>-->
      <a-button @click="mothSourceDown" type="primary">批量下载源文件</a-button>
      <a-button @click="mothAfresh" type="primary" v-has="'monthReport:dayAfresh'" >重新报送</a-button>
      <!--<a-button @click="mothRepulse" type="primary" v-has="'monthReport:dayRepulse'" >打回</a-button>-->
      <!--<a-button @click="dayDelete" type="primary"  v-has="'report:dayRepulse'" >删除</a-button>-->
      <a-button @click="handleUpload" type="primary">报文上报</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="reprotDetailList(record)">{{record.BANKNAME}}</a>
        </span>
      </a-table>
    </div>
     <day-report-item-list ref="dayReportItemList"></day-report-item-list>
    <day-repor-modal ref="modalForm" @ok="modalFormOk"></day-repor-modal>
    <j-modal
      :visible.sync="modal.visible"
      :width="600"
      :title="modal.title"
      :fullscreen.sync="modal.fullscreen"
      :switchFullscreen="modal.switchFullscreen"
    >
      <template v-for="(i,k) in dataList">
        <p :key="k">{{i}}</p>
      </template>
      <template slot="footer">
        <a-button @click="this.closeWindow" type="primary">关闭</a-button>
      </template>
    </j-modal>
    <j-modal
      :visible.sync="modal.visible1"
      :width="750"
      :title="modal.title1"
      :fullscreen.sync="modal.fullscreen1"
      :switchFullscreen="modal.switchFullscreen1"
    >
      <template>
          <a-form-item :required="true">
            数据日期:&nbsp;&nbsp;&nbsp;<j-date placeholder="请选择数据日期" v-model="sjrq"></j-date>
          </a-form-item>
          <a-table :rowSelection="{  selectedRowKeys: selectedRowKeys1, onChange: onSelectChange1 }"
                   bordered
                   rowKey="reporttypecode"
                   :columns="columns1"
                   :dataSource="dataList1">
          </a-table>
      </template>
      <template slot="footer">
        <a-button @click="this.createAll" type="primary">全部生成</a-button>
        <a-button @click="this.createSel" type="primary">生成选择项</a-button>
        <a-button @click="this.closeWindow1" type="primary">关闭</a-button>
      </template>
    </j-modal>
  </a-card>
</template>

<script>
  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import DayReporModal from './modules/MonthReporModal'
  import DayReportItemList from './MonthReportItemList'
  import {getAction} from '@/api/manage'
  import { ACCESS_TOKEN} from "@/store/mutation-types"
  import Vue from 'vue'

  export default {
    name: 'DayReporList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      DayReporModal,
      DayReportItemList
    },
    data () {
      return {
        modal: {
          title: '未提交业务表',
          visible: false,
          fullscreen: false,
          switchFullscreen: false,
          title1: '业务表',
          visible1: false,
          fullscreen1: false,
          switchFullscreen1: false,
        },
        selectedRowKeys1:[],
        selectionRows1:[],
        sjrq:'',
        dataList:[],
        dataList1:[],
        description: '日报报文',
        monthFormat: 'YYYY-MM',
        // 表头
        columns: [
          {
            title: '选项',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'报送任务日期',
            align:"center",
            dataIndex: 'REPORTTASKDATE'
          },
          {
            title:'数据日期',
            align:"center",
            dataIndex: 'DATADATE'
          },
          {
            title: '法人名称',
            dataIndex: 'action',
            align: "center",
            scopedSlots: {
              customRender: 'action'},
          },
          {
            title:'主题名称',
            align:"center",
            dataIndex: 'THEMENAME'
          },
          {
            title:'总分片数',
            align:"center",
            dataIndex: 'TOTALSEPARATE'
          },
          {
            title:'总记录行',
            align:"center",
            dataIndex: 'TOTALROWNUM'
          },
          {
            title:'生成成功',
            align:"center",
            dataIndex: 'CREATESTATE'
          },
          {
            title:'校验成功',
            align:"center",
            dataIndex: 'VERIFYSTATE'
          },
          {
            title:'上报成功',
            align:"center",
            dataIndex: 'REPORTSTATE'
          },
          {
            title:'重报成功',
            align:"center",
            dataIndex: 'REPORTFLAG'
          },
          {
            title:'反馈成功',
            align:"center",
            dataIndex: 'FEEDBACK'
          },
          {
            title: '操作时间',
            align: "center",
            dataIndex: 'UPDATE_TIME',
          },/*,
          {
            title: '法人名称',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }*/
        ],
        columns1: [
          {
            title: '选项',
            dataIndex: '',
            key:'rowIndex',
            width:70,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'采集报表名称',
            align:"left",
            dataIndex: 'reporttypename'
          }
       ],
        queryParam: {
          sjrq: "",
          bankNoCd: "",
          theme:""
        },
        url: {
          list: "/messagemanagement/llbbMessageManagement/findDayReportPageListNew",
          delete: "/corporateCustomer/llbbCorporateCustomer/delete",
          deleteBatch: "/corporateCustomer/llbbCorporateCustomer/deleteBatch",
          exportXlsUrl: "/corporateCustomer/llbbCorporateCustomer/exportXls",
          importExcelUrl: "corporateCustomer/llbbCorporateCustomer/importExcel",

        },
        dictOptions:{},
        superFieldList:[],
        checkPagam:{
          sjrq:[],
          bankNoCd:[]
        }
      }
    },
    created() {
    this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      dayDelete(){
        const that = this;
        if(this.selectedRowKeys.length==0){
          that.$message.warning("请选择你要删除的数据");
          return false;
        }
        this.checkPagam={
          sjrq:[],
          bankNoCd:[]
        }
        for(var i=0;i<this.selectedRowKeys.length;i++){
          this.checkPagam.sjrq[i]=this.dataSource[this.selectedRowKeys[i]].DATADATE;
          this.checkPagam.bankNoCd[i]=this.dataSource[this.selectedRowKeys[i]].BANKCODE;
        }
        let url="/messagemanagement/llbbMessageManagement/dayDelete";
        this.loading = true;

        getAction(url,this.checkPagam).then((res)=>{
          that.onClearSelected();
          this.loading = false;
          if(res.success){
            that.$message.success(res.message);
            this.loadData(1);
          }else{
            that.$message.warning(res.message);
          }
        }).finally(() => {

        })
      },
      onSelectChange1(selectedRowKeys, selectionRows) {
        this.selectedRowKeys1 = selectedRowKeys;
        this.selectionRows1 = selectionRows;
      },
      closeWindow(){
        this.modal.visible=false;
      },
      closeWindow1(){
        this.modal.visible1=false;
      },
      createAll(){
        if(this.sjrq==''){
          this.$message.warning("日期不能为空！");
          return;
        }
        var that=this;
        var vurl='/messagemanagement/llbbMessageManagement/spInfo?indate='+this.sjrq+'&codes[]=';
        getAction(vurl).then((res)=>{
          that.loading = false;
          if(res.success){
            that.dataList1=res.result;
            that.$message.info("生成成功")
          }else{
            that.$message.warning(res.message);
            return;
          }
          that.modal.visible1=true;
          that.sjrq='';
          that.selectedRowKeys1 = [];
          that.selectionRows1 = [];
          that.modal.visible1=false;
        });
      },
      createSel(){
        if(this.sjrq==''){
          this.$message.warning("日期不能为空！");
          return;
        }
        var that=this;
        if(this.selectedRowKeys1.length==0){
          this.$message.warning("请选择采集报表！");
          return;
        }
        var vurl='/messagemanagement/llbbMessageManagement/spInfo?indate='+this.sjrq+'&codes[]=';
        vurl+=this.selectedRowKeys1;
        getAction(vurl).then((res)=>{
          that.loading = false;
          if(res.success){
            that.dataList1=res.result;
            that.$message.info("生成成功")
          }else{
            that.$message.warning(res.message);
            return;
          }
          that.modal.visible1=true;
          that.sjrq='';
          that.selectedRowKeys1 = [];
          that.selectionRows1 = [];
          that.modal.visible1=false;
        });
      },
      bathCreate(){
        const that = this;
        let url="/llbbMessageForms/llbbMessageForms/nlist";
        this.loading = true;
        getAction(url).then((res)=>{
          that.loading = false;
          if(res.success){
            that.dataList1=res.result;
          }else{
            that.$message.warning(res.message);
          }
          that.modal.visible1=true;
          that.sjrq='';
          that.selectedRowKeys1 = [];
          that.selectionRows1 = [];
        });
      },
      mothDown(){
        const that = this;
        if(this.selectedRowKeys.length==0){
          that.$message.warning("请选择你要批量下载的数据");
          return false;
        }
        for(var i=0;i<this.selectedRowKeys.length;i++){
          this.checkPagam={
            sjrq:[],
            bankNoCd:[]
          }
          this.checkPagam.sjrq[0]=this.dataSource[this.selectedRowKeys[i]].DATADATE;
          this.checkPagam.bankNoCd[0]=this.dataSource[this.selectedRowKeys[i]].BANKCODE;
          let url=`${window._CONFIG['domianURL']}`+"/messagemanagement/llbbMessageManagement/dayDownLoad?sjrq[]="
            +this.checkPagam.sjrq+"&bankNoCd[]="+this.checkPagam.bankNoCd+"&token="+Vue.ls.get(ACCESS_TOKEN);
          try {
            var elemIF = document.createElement("iframe");
            elemIF.src = url;
            elemIF.style.display = "none";
            document.body.appendChild(elemIF);
          } catch (e) {}
        }
        that.onClearSelected();
        /*let url=`${window._CONFIG['domianURL']}`+"/messagemanagement/llbbMessageManagement/mothDownLoad?sjrq[]="+ this.checkPagam.sjrq+"&bankNoCd[]="+this.checkPagam.bankNoCd;
        window.location.href=url;*/
        /*let url="/messagemanagement/llbbMessageManagement/mothDownLoad";
        let data=new Date();
        downloadFile(url,data.getTime()+".zip",this.checkPagam);*/
        setTimeout(function(){
          that.loadData(1);
        },3000);
      },
      mothSourceDown(){
        const that = this;
        if(this.selectedRowKeys.length==0){
          that.$message.warning("请选择你要批量下载的数据");
          return false;
        }
        for(var i=0;i<this.selectedRowKeys.length;i++){
          this.checkPagam={
            sjrq:[],
            bankNoCd:[]
          }
          this.checkPagam.sjrq[0]=this.dataSource[this.selectedRowKeys[i]].DATADATE;
          this.checkPagam.bankNoCd[0]=this.dataSource[this.selectedRowKeys[i]].BANKCODE;
          let url=`${window._CONFIG['domianURL']}`+"/messagemanagement/llbbMessageManagement/daySourceDown?sjrq[]="+
              this.checkPagam.sjrq+"&bankNoCd[]="+this.checkPagam.bankNoCd+"&token="+Vue.ls.get(ACCESS_TOKEN);
          try {
            var elemIF = document.createElement("iframe");
            elemIF.src = url;
            elemIF.style.display = "none";
            document.body.appendChild(elemIF);
          } catch (e) {}
        }
        that.onClearSelected();
        setTimeout(function(){
          that.loadData(1);
        },3000);
      },
      mothAfresh(){
        const that = this;
        if(this.selectedRowKeys.length==0){
          that.$message.warning("请选择你要重新报送的数据");
          return false;
        }
        this.checkPagam={
          sjrq:[],
          bankNoCd:[]
        }
        for(var i=0;i<this.selectedRowKeys.length;i++){
          this.checkPagam.sjrq[i]=this.dataSource[this.selectedRowKeys[i]].DATADATE;
          this.checkPagam.bankNoCd[i]=this.dataSource[this.selectedRowKeys[i]].BANKCODE;
        }
        let url="/messagemanagement/llbbMessageManagement/mothAfresh";
        this.loading = true;
        getAction(url,this.checkPagam).then((res)=>{
          that.onClearSelected();
          this.loading = false;
          if(res.success){
            that.$message.success(res.message);
            this.loadData(1);
          }else{
            that.$message.warning(res.message);
          }
        }).finally(() => {

        })
      },
      mothRepulse(){
        const that = this;
        if(this.selectedRowKeys.length==0){
          that.$message.warning("请选择你要批量校验的数据");
          return false;
        }
        this.checkPagam={
          sjrq:[],
          bankNoCd:[]
        }
        for(var i=0;i<this.selectedRowKeys.length;i++){
          this.checkPagam.sjrq[i]=this.dataSource[this.selectedRowKeys[i]].DATADATE;
          this.checkPagam.bankNoCd[i]=this.dataSource[this.selectedRowKeys[i]].BANKCODE;
        }
        let url="/messagemanagement/llbbMessageManagement/mothRepulse";
        this.loading = true;
        getAction(url,this.checkPagam).then((res)=>{
          that.onClearSelected();
          this.loading = false;
          if(res.success){
            that.$message.success(res.message);
            this.loadData(1);
          }else{
            that.$message.warning(res.message);
          }
        }).finally(() => {

        })
      },
      mothCheck(){
        const that = this;
        if(this.selectedRowKeys.length==0){
          that.$message.warning("请选择你要批量校验的数据");
          return false;
        }
        this.checkPagam={
          sjrq:[],
          bankNoCd:[]
        }
        for(var i=0;i<this.selectedRowKeys.length;i++){
          this.checkPagam.sjrq[i]=this.dataSource[this.selectedRowKeys[i]].DATADATE;
          this.checkPagam.bankNoCd[i]=this.dataSource[this.selectedRowKeys[i]].BANKCODE;
        }
        let url="/messagemanagement/llbbMessageManagement/mothCheck";
        this.loading = true;
        getAction(url,this.checkPagam).then((res)=>{
          that.onClearSelected();
          this.loading = false;
          if(res.success){
            that.$message.success(res.message);
            this.loadData(1);
          }else{
            that.$message.warning(res.message);
          }
        }).finally(() => {

        })
      },
      bathSave(){
        const that = this;
        if(this.queryParam.sjrq==""){
          that.$message.warning("请选择数据日期");
          return false;
        }
        let url="/messagemanagement/llbbMessageManagement/dayReportNew";
        this.loading = true;
        getAction(url,this.queryParam).then((res)=>{
          that.onClearSelected();
          this.loading = false;
          if(res.success){
            that.$message.success(res.message);
            setTimeout(function(){
              that.loadData(1);
            },1500);
          }else{
            if(res.result==null){
              that.$message.error(res.message);
            }else{
              if(res.result==null){
                that.$message.error(res.message);
              }else if(res.code == 501){
                this.dataList=res.result;
                this.modal.title='未提交业务表'
                this.modal.visible=true;
              }else if(res.code == 502){
                this.dataList=res.result;
                this.modal.title='未表间校验业务表'
                this.modal.visible=true;
              }else if(res.code == 503){
                this.dataList=res.result;
                this.modal.title='未锁库业务表'
                this.modal.visible=true;
              }
            }
          }
        }).finally(() => {

        })
      },
      bathSaveTest(){
        const that = this;
        if(this.queryParam.sjrq==""){
          that.$message.warning("请选择数据日期");
          return false;
        }
        let url="/messagemanagement/llbbMessageManagement/dayReportNewTest";
        this.loading = true;
        getAction(url,this.queryParam).then((res)=>{
          that.onClearSelected();
          this.loading = false;
          if(res.success){
            that.$message.success(res.message);
            setTimeout(function(){
              that.loadData(1);
            },1500);
          }else{
            if(res.result==null){
              that.$message.error(res.message);
            }else{
              if(res.result==null){
                that.$message.error(res.message);
              }else if(res.code == 501){
                this.dataList=res.result;
                this.modal.title='未提交业务表'
                this.modal.visible=true;
              }else if(res.code == 502){
                this.dataList=res.result;
                this.modal.title='未表间校验业务表'
                this.modal.visible=true;
              }else if(res.code == 503){
                this.dataList=res.result;
                this.modal.title='未锁库业务表'
                this.modal.visible=true;
              }
            }
          }
        }).finally(() => {

        })
      },
      handleUpload() {
        const that = this;
        if(this.selectedRowKeys.length==0){
          that.$message.warning("请选择你要处理的数据");
          return false;
        }
        let ids="";
        for(var i=0;i<this.selectionRows.length;i++){
          let sjrq=this.selectionRows[i].DATADATE;
          let theme=this.selectionRows[i].THEME;
          let bankNoCd=this.selectionRows[i].BANK_NO_CD;
          ids = sjrq+"@"+theme+"@"+bankNoCd+","
        }
        var vurl =`${window._CONFIG['domianURL']}/messagemanagement/llbbMessageManagement/sendFtp?ids=`+ids;
        this.loading = true;
        getAction(vurl,this.checkPagam).then((res)=>{
          that.onClearSelected();
          that.loading = false;
          if(res.success){
            that.$message.success(res.message);
            that.loadData(1);
          }else{
            that.$message.warning(res.message);
          }
        }).finally(() => {
        })
      },
      onChange(date, dateString) {
        this.queryParam.sjrq = dateString
        let sjrq=dateString;
        sjrq=sjrq.replace("-","")
        let yearMoth=this.getYearMoth();
        if(sjrq>=yearMoth){
          this.$message.warning("数据日期不能大于当前日期，请重新选择");
          this.queryParam.sjrq = ''
        }

      },
      getYearMoth(){
        var myDate = new Date();
        var tYear = myDate.getFullYear();
        var tMonth = myDate.getMonth();

        var m = tMonth + 1;
        if (m.toString().length == 1) {
          m = "0" + m;
        }
        var yearMoth= tYear +'-'+ m;
        yearMoth=yearMoth.replace("-","")
        return yearMoth;
      },
      initDictConfig(){
      },
      reprotDetailList(record){
        this.$refs.dayReportItemList.edit(record);
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'bankNoCd',text:'法人标识'})
        fieldList.push({type:'date',value:'sjrq',text:'数据日期'})
        fieldList.push({type:'string',value:'khh',text:'客户号'})
        fieldList.push({type:'string',value:'nbjgh',text:'内部机构号'})
        fieldList.push({type:'string',value:'gmjjbmfl',text:'国民经济部门分类'})
        fieldList.push({type:'string',value:'jrjglxdm',text:'金融机构类型代码'})
        fieldList.push({type:'string',value:'qygm',text:'企业规模'})
        fieldList.push({type:'string',value:'kglx',text:'控股类型'})
        fieldList.push({type:'string',value:'jnjwbz',text:'境内境外标志'})
        fieldList.push({type:'string',value:'jyszdxzqhdm',text:'经营所在地行政区划代码'})
        fieldList.push({type:'string',value:'zcdz',text:'注册地址'})
        fieldList.push({type:'number',value:'sxed',text:'授信额度'})
        fieldList.push({type:'number',value:'yyed',text:'已用额度'})
        fieldList.push({type:'string',value:'sshy',text:'所属行业'})
        fieldList.push({type:'string',value:'nccsbz',text:'农村城市标志'})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>